package fri.pipt.agent.deathrow;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

import fri.pipt.agent.indy.Indy;
import fri.pipt.protocol.Neighborhood;

public class VisualDebugger extends JFrame
{
	public final static int TILE_SIZE = 10;
	
	AgentAbstraction agent = null;
	
	private ArenaPanel panel;
	
	private class ArenaPanel extends JPanel
	{
		
	}
	
	public VisualDebugger( AgentAbstraction agent)
	{
		this.agent = agent;
		setSize(new Dimension(800,800));
		panel = new ArenaPanel();
		setLayout(new BorderLayout());
		add(BorderLayout.CENTER, panel);
		setTitle("Visual debugger: getId() = " + agent.getId());
		setVisible(true);
	}
	
	public void renderObservableArea()
	{
		//panel.getGraphics().clearRect(0, 0, panel.getWidth(), panel.getHeight());
		World world = agent.getWorld();
		ObservableArea oa = world.getObservationArea();
		for(int x = oa.minX; x <= oa.maxX; x++)
		{
			for(int y = oa.minY; y <= oa.maxY; y++)
			{
				int screenPosX = x - oa.minX;
				int screenPosY = y - oa.minY;
				renderTile(screenPosX, screenPosY, TILE_SIZE, world.getTile(x, y));
			}
		}
		//renderTile(agent.getPosition().x, agent.getPosition().y, TILE_SIZE,/* world.getTile(x, y)*/ World.TILE_AGENT_ME);
	}
	
	private void renderTile(int x, int y, int size, int tile)
	{
		Graphics g = panel.getGraphics();
		Color tileColor = Color.BLACK;
		Color tileBorder = Color.WHITE;
		int absuluteX = x * size;
		int absuluteY = y * size;
		boolean useOval = false;
		
		if(tile == World.TILE_EMPTY) //Prazno
			tileColor = Color.GREEN;
		else if(tile == World.TILE_WALL) //Stena
			tileColor = Color.GRAY;
		else if(tile == 0) //Neraziskano področje
			tileColor = Color.BLACK;
		else if(tile == World.TILE_HEADQUARTERS) //Baza
			tileColor = Color.RED;
		
		if(!useOval)
		{
			g.setColor(tileColor);
			g.fillRect(absuluteX, absuluteY, size, size);
			g.setColor(tileBorder);
			g.drawRect(absuluteX, absuluteY, size, size);
		}
		if(tile == World.TILE_AGENT_ME)
		{
			g.setColor(Color.YELLOW);
			g.fillOval(absuluteX, absuluteY, size, size);
		}
		
	}
}
